package main

import (
	"context"
	"fmt"
	"go-gin-example/middleware/logger"
	"go-gin-example/models"
	"go-gin-example/pkg/gredis"
	"net/http"
	"os"
	"os/signal"
	"syscall"
	"time"

	"go-gin-example/pkg/setting"
	"go-gin-example/routers"

	"go.uber.org/zap"
)

// @title go-gin-example Swagger
// @version 2.0 版本
// @description go-gin-example Service 描述
// @query.collection.format multi
func main() {

	setting.Setup()
	models.Setup()
	logger.Setup()
	_ = gredis.Setup()
	router := routers.InitRouter()

	s := &http.Server{
		Addr:           fmt.Sprintf(":%d", setting.ServerSetting.HttpPort),
		Handler:        router,
		ReadTimeout:    setting.ServerSetting.ReadTimeout,
		WriteTimeout:   setting.ServerSetting.WriteTimeout,
		MaxHeaderBytes: 1 << 20,
	}

	go func() {
		if err := s.ListenAndServe(); err != nil {
			zap.L().Info("Listen", zap.String("error", err.Error()))
		}
	}()
	zap.L().Info("Starting Server...")
	quit := make(chan os.Signal, 1)
	signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
	<-quit
	zap.L().Info("Shutdown Server...")

	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()
	if err := s.Shutdown(ctx); err != nil {
		zap.L().Info("Shutdown Server...", zap.String("err", err.Error()))
	}
	zap.L().Info("Server exiting")
}
